package com.xiaoyu.base.dao;

import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.Query;

import com.xiaoyu.base.DBCall;
import com.xiaoyu.base.entity.ConversationEntity;
import com.xiaoyu.base.entity.MessageEntity;

import java.util.List;

import in.srain.cube.request.JsonData;

import static androidx.room.OnConflictStrategy.REPLACE;

@Dao
public abstract class ConversationDao {

    @Insert(onConflict = REPLACE)
    public abstract long upsert(ConversationEntity conversation);

    @Insert(onConflict = REPLACE)
    public abstract List<Long> upsertAll(List<ConversationEntity> conversations);

    @Query("SELECT * FROM conversation WHERE chatId = :chatId LIMIT 1")
    public abstract ConversationEntity getConversation(String chatId);

    @Query("SELECT * FROM conversation ORDER BY lastUpdateTime DESC, ROWID ASC")
    public abstract List<ConversationEntity> getAllConversations();

    @Query("DELETE FROM conversation WHERE chatId = :chatId")
    public abstract void delete(String chatId);

    @Query("DELETE FROM conversation")
    public abstract void deleteAll();

    @Query("UPDATE conversation SET lastMessageId = :lastMessageId, lastUpdateTime = :lastMessageTime WHERE chatId = :chatId")
    public abstract void updateLastMessage(String chatId, String lastMessageId, long lastMessageTime);

    @Query("UPDATE conversation SET unreadCount = :unreadCount WHERE chatId = :chatId")
    public abstract void updateUnreadCount(String chatId, int unreadCount);
   
    @Query("UPDATE conversation SET status = (status & ~:mask) | (:status & :mask) WHERE chatId = :chatId")
    public abstract void updateStatus(String chatId, int mask, int status);

    @Query("UPDATE conversation SET topWeight = :topWeight WHERE chatId = :chatId")
    public abstract void updateTopWeight(String chatId, long topWeight);

    public static ConversationEntity getOrDefault(String chatId) {
        ConversationEntity entity = DBCall.call(database -> database.conversationDao().getConversation(chatId));
        return entity == null ? new ConversationEntity() : entity;
    }

    public void updateLastMessage(String chatId) {
        MessageEntity messageEntity = DBCall.call(database -> database.messageDao().getNewestLastContentMessage(chatId));
        String lastMessageId = messageEntity == null ? null : messageEntity.getId();
        long lastMessageTime = messageEntity == null ? 0 : messageEntity.getDisplayTime();
        updateLastMessage(chatId, lastMessageId, lastMessageTime);
    }

    public static ConversationEntity updateEntity(JsonData jsonData) {
        final String chatId = jsonData.optString("chatId");
        ConversationEntity entity = getOrDefault(chatId);
        entity.setChatId(chatId);
        entity.setType(jsonData.optString("type"));
        entity.setToUserAccid(jsonData.optString("toUserAccid"));
        entity.setName(jsonData.optString("name"));
        entity.setIcon(jsonData.optString("icon"));
        entity.setToUid(jsonData.optJson("toUser").optString("id"));
        entity.setExt(jsonData.optJson("ext").toString());
        entity.setStatus(jsonData.optInt("status"));
        return entity;
    }
}
